<?php
/**
 * 我的相关处理
 * @author ice<iceinto@mallog.com.cn>
 */
class MyAction extends CommonAction
{
    public function _initialize()
    {
        parent::_initialize();
        $this->assign('web_title', '我的任务');
        $this->assign('nav', 2);
    }

    /**
     * 任务首页
     * @author ice<iceinto@mallog.com.cn>
     */
    public function index()
    {
        $uid = $this->getLoginUserInfo();
        $QueueModel = D('Queue');
        $MallModel = D('Mall');
        $nowTask = $QueueModel->getNowTask($uid);
        $brand = $MallModel->getBrand($nowTask['bid']);
        $this->assign('brand', $brand);
        $this->assign('nowTask', $nowTask);
        $this->display();
    }

    /**
     * Task Details
     * @author ice<iceinto@mallog.com.cn>
     */
    public function task()
    {
        //Judge the current task whether meet the requirements
        $TaskModel = D('Task');
        $tid = intval(getGet('id'));
        $uid = $this->getLoginUserInfo();

        $task = $TaskModel->isNowReceive($tid);
        $nowTask = D('Queue')->getNowTask($uid);
        if ($nowTask['id'] != $tid) {
            $this->error('当前执行的任务不属于你');
        }
        if (!$task) {
            $this->error(L('_CURRENT_NOT_ALLOWED'));
        }
        $TaskAllotModel = D('TaskAllot');
        $allot = $TaskAllotModel->getOneByTid($tid, 'id,create_time,status,start_time');
        if (isPost()) {
            $status = intval(getPost('status'));
            if ($status == 3) {
                //task start
                $TaskAllotModel->startTime($tid);
                $this->updateStatus($tid, 3);
                $allot = $TaskAllotModel->getOneByTid($tid, 'id,create_time,status,start_time');
            } else if ($status == 4) {
                //task return
                $remark = hh(getPost('remark'));
                if ($remark) {
                    //add return
                    $data = array();
                    $data['tid'] = $tid;
                    $data['remark'] = $remark;
                    $data['uid'] = $this->getLoginUserInfo();
                    $data['create_time'] = time();

                    if (M('taskReturn')->add($data)) {
                        //Add task return
                        $this->updateStatus($tid, 4);
                    }
                } else {
                    $this->error(L('_REMARK_MUST'));
                }
            } else if ($status == 6) {
                if (!$allot) {
                    $this->error(L('_UPDATE_LOSE'));
                }
                $remark = hh(getPost('remark'));
                if (!$remark) {
                    $this->error(L('_REMARK_MUST'));
                }
                $fid = $this->uploadFiles('task');
                $data = array();
                $data['id'] = $allot['id'];
                $data['end_time'] = time();
                $data['remark'] = $remark;
                $data['image'] = $fid;
                if ($TaskAllotModel->save($data)) {
                    //Update status
                    $this->updateStatus($tid, 6);
                    //Jump to list page
                    redirect(U('/Task/My/'));
                } else {

                    $this->error(L('_UPDATE_LOSE'));
                }
            }
            $task = $TaskModel->isNowReceive($tid);
        }
        $returns = D('TaskReturn')->getAllReturn($tid);
        $task['id'] = $tid;
        $task = D('Mall')->setAddress($task);
        $queue = D('Queue')->getOneById($tid);
        $this->assign('allot', $allot);
        $this->assign('queue', $queue);
        $this->assign('returns', $returns);
        $this->assign('task', $task);
        $this->assign('web_title', $task['id'] . ' - ' . $task['title']);
        $this->display();
    }

    /**
     * Modify return information
     * @author Ice<iceinto@mallog.com.cn>
     */
    public function auditReturn()
    {
        $tid = intval(getGet('id'));

        $TaskModel = D('Task');
        $TaskAuditReturnModel = D('TaskAuditReturn');
        $TaskAllotModel = D('TaskAllot');
        $task = $TaskModel->find($tid);
        $queueStatus = D('Queue')->getStatusById($task['id']);
        if ($queueStatus != 2) {
            $this->error('当前状态错误！');
        }
        $taskAllot = $TaskAllotModel->getOneByTid($tid, '*');
        $taskImage = $this->getManagedUrl($taskAllot['image'], 'task');
        $auditReturn = $TaskAuditReturnModel->getReturnOne($tid);

        if ($auditReturn) {
            $user = D('User')->getById($auditReturn['uid'], 'num');
            $auditReturn['num'] = $user['num'];
        }

        if ($task && $auditReturn && $taskAllot && $taskImage) {
        } else {
            $this->error(L('_DATE_ERROR'));
        }

        if (isPost()) {
            $data = array();
            $file = $_FILES['image'];
            if (!empty($file['name'])) {
                $data['id'] = $taskAllot['id'];
                $data['image'] = $this->uploadFiles('task');
                //Update task allot
                if ($TaskAllotModel->save($data)) {
                    $data['image'] = $taskAllot['image'];
                }
            }

            $data['id'] = $auditReturn['id'];
            $data['reply'] = hh(getPost('remark'));
            $data['reply_time'] = time();
            $data['status'] = 2;

            if (empty($data['reply'])) {
                $this->error(L('_REMARK_MUST'));
            }
            //Update task audit return
            if ($TaskAuditReturnModel->save($data)) {
                $this->updateStatus($tid, 6);
            }
            $this->redirect('/My/', null);
        }

        $this->assign('task', $task);
        $this->assign('auditReturn', $auditReturn);
        $this->assign('taskAllot', $taskAllot);
        $this->assign('taskImage', $taskImage);
        $this->display();
    }

    /**
     * Update queue status
     * @param int $tid
     * @param int $status
     * @param int $auditor
     * @author ice<iceinto@mallog.com.cn>
     */
    public function updateStatus($tid, $status, $auditor = null)
    {
        D('Queue')->updateStatus($tid, $status, $auditor);
    }
    /**
     *         $QueueModel = D('Queue');
    //获取是否有审核返回信息
    $auditReturn = D('TaskAuditReturn')->getReturnOne($tid);
    $queue = $QueueModel->getOneById($tid);
    //判断是否是审核被退回的信息
    if($auditReturn){
    $queue['status'] = 7;
    $auditReturn['num'] = D('User')->getNum($auditReturn['uid']);
    $this->assign('auditReturn', $auditReturn);
    }
     */
}

?>